/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.dummydevice.monitors;

import javax.swing.BorderFactory;
import javax.swing.JScrollPane;

import net.zarubsys.robotplayground.core.monitors.IMonitorListener;
import net.zarubsys.robotplayground.dummydevice.monitors.DummyPositionMonitor;
import net.zarubsys.robotplayground.dummydevice.monitors.DummyPostionMonitorEventData;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

/**
 * DummyPositionMapView
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class DummyPositionMapView extends ViewPart implements IMonitorListener<DummyPostionMonitorEventData> {
	
	public static final String ID = "net.zarubsys.robotplayground.gui.dummydevice.mapView";
	
	private Composite mainComposite;
	
	private DummyPositionMapViewPanel panel;
	
	public void initMonitor(DummyPositionMonitor positionMonitor) {
		panel.setDummyPositionMonitor(positionMonitor);
		mainComposite.setSize(panel.getRequiredSize());
		mainComposite.getParent().layout(true, true);
	}
	
	@Override
	public void createPartControl(Composite parent) {
		ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
		mainComposite = new Composite(scrolledComposite, SWT.EMBEDDED);
		scrolledComposite.setContent(mainComposite);
		
		java.awt.Frame frame = SWT_AWT.new_Frame(mainComposite);
	    panel = new DummyPositionMapViewPanel();
	    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
	    JScrollPane scrollPane = new JScrollPane(panel);
	    frame.add(scrollPane);
	    
	}

	public void notifyListener(final DummyPostionMonitorEventData data) {
		Display.getDefault().asyncExec(new Runnable() {
			
			@Override
			public void run() {
				panel.update(data);
			}
			
		});
	}

	@Override
	public void setFocus() {
		getSite().getPage().activate(this);
	}

}

